package erfgame.core.world.terrain;

import java.io.Serializable;


public class BlockGridTiles implements Serializable {
	
	private static final long serialVersionUID = 9831475; 
	
	public static final int INDEX_NE = 0;
	public static final int INDEX_SE = 1;
	public static final int INDEX_SW = 2;
	public static final int INDEX_NW = 3;
	public static final int INDEX_UP = 4;
	public static final int INDEX_DOWN = 5;
	
	public static final int MAX_INDEX = 3;
	
	public static final byte MOVE_TYPE_WALK 	= 1;
	public static final byte MOVE_TYPE_FLY		= 2;
	public static final byte MOVE_TYPE_SWIM 	= 4;
	public static final byte MOVE_TYPE_SUBMERGE = 8;
	
	private GridTile[][][] tiles;
	private WalkablePath[][][] paths;
	private int bx;
	private int by;
	
	public BlockGridTiles(int bx, int by, GridTile[][][] tiles, WalkablePath[][][] paths) {
		this.bx = bx;
		this.by = by;
		this.tiles = tiles;
		this.paths = paths;
	}
	
	public void setPaths( WalkablePath[][][] paths ) {
		this.paths = paths;
	}
	
	public WalkablePath getPath( int px, int py, int index ) {
		return this.paths[px][py][index];
	}
	
	public WalkablePath[][][] getPaths() {
		return this.paths;
	}
	
	public WalkablePath[] getPaths( int px, int py ) {
		return this.paths[px][py];
	}
	
	public GridTile getGridTile( int gx, int gy, int index ) {
		return this.tiles[gx][gy][index];
	}
	
	public GridTile[][][] getGridTiles() {
		return this.tiles;
	}
	
	public GridTile findGridTile( int gx, int gy, int tz) {
		// locate the path with the closest (should be equal) tz
		GridTile[] tiles = this.tiles[gx][gy];
		// TODO : binary search or indexing
		for( int i=tiles.length; i>0; ) {
			i--;
			GridTile tile = tiles[i];
			int foundTz = tile.getZ();
			if( foundTz <= tz ) {
				return tile;
			}
		}
		return null;
	}
	
	public GridTile[] getGridTiles( int gx, int gy ) {
		return this.tiles[gx][gy];
	}
	
	public int getBx() {
		return this.bx;
	}
	
	public int getBy() {
		return this.by;
	}
}
